from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession

from module_admin.entity.do.kq_attendance_do import KhAttendance
from module_admin.entity.vo.kq_attendance_vo import Kq_attendanceModel, Kq_attendancePageQueryModel
from utils.page_util import PageUtil


class Kq_attendanceDao:
    """
    考勤数据表模块数据库操作层
    """

    @classmethod
    async def get_kq_attendance_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据序号获取考勤数据表详细信息

        :param db: orm对象
        :param id: 序号
        :return: 考勤数据表信息对象
        """
        kq_attendance_info = (
            (
                await db.execute(
                    select(KhAttendance)
                    .where(
                        KhAttendance.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return kq_attendance_info

    @classmethod
    async def get_kq_attendance_detail_by_info(cls, db: AsyncSession, kq_attendance: Kq_attendanceModel):
        """
        根据考勤数据表参数获取考勤数据表信息

        :param db: orm对象
        :param kq_attendance: 考勤数据表参数对象
        :return: 考勤数据表信息对象
        """
        kq_attendance_info = (
            (
                await db.execute(
                    select(KhAttendance).where(
                        KhAttendance.user_id == kq_attendance.user_id if kq_attendance.user_id else True,
                        KhAttendance.user_name == kq_attendance.user_name if kq_attendance.user_name else True,
                        KhAttendance.am_pm == kq_attendance.am_pm if kq_attendance.am_pm else True,
                        KhAttendance.kh_work == kq_attendance.kh_work if kq_attendance.kh_work else True,
                        KhAttendance.punch_date == kq_attendance.punch_date if kq_attendance.punch_date else True,
                        KhAttendance.punch_time == kq_attendance.punch_time if kq_attendance.punch_time else True,
                        KhAttendance.punch_result == kq_attendance.punch_result if kq_attendance.punch_result else True,
                    )
                )
            )
            .scalars()
            .first()
        )

        return kq_attendance_info

    @classmethod
    async def get_kq_attendance_list(cls, db: AsyncSession, query_object: Kq_attendancePageQueryModel, is_page: bool = False):
        """
        根据查询参数获取考勤数据表列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 考勤数据表列表信息对象
        """
        query = (
            select(KhAttendance)
            .where(
                KhAttendance.user_id == query_object.user_id if query_object.user_id else True,
                KhAttendance.user_name.like(f'%{query_object.user_name}%') if query_object.user_name else True,
                KhAttendance.am_pm == query_object.am_pm if query_object.am_pm else True,
                KhAttendance.kh_work == query_object.kh_work if query_object.kh_work else True,
                KhAttendance.punch_date == query_object.punch_date if query_object.punch_date else True,
                KhAttendance.punch_time == query_object.punch_time if query_object.punch_time else True,
                KhAttendance.punch_result == query_object.punch_result if query_object.punch_result else True,
            )
            .order_by(KhAttendance.id)
            .distinct()
        )
        kq_attendance_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return kq_attendance_list

    @classmethod
    async def add_kq_attendance_dao(cls, db: AsyncSession, kq_attendance: Kq_attendanceModel):
        """
        新增考勤数据表数据库操作

        :param db: orm对象
        :param kq_attendance: 考勤数据表对象
        :return:
        """
        db_kq_attendance = KhAttendance(**kq_attendance.model_dump(exclude={}))
        db.add(db_kq_attendance)
        await db.flush()

        return db_kq_attendance

    @classmethod
    async def edit_kq_attendance_dao(cls, db: AsyncSession, kq_attendance: dict):
        """
        编辑考勤数据表数据库操作

        :param db: orm对象
        :param kq_attendance: 需要更新的考勤数据表字典
        :return:
        """
        await db.execute(update(KhAttendance), [kq_attendance])

    @classmethod
    async def delete_kq_attendance_dao(cls, db: AsyncSession, kq_attendance: Kq_attendanceModel):
        """
        删除考勤数据表数据库操作

        :param db: orm对象
        :param kq_attendance: 考勤数据表对象
        :return:
        """
        await db.execute(delete(KhAttendance).where(KhAttendance.id.in_([kq_attendance.id])))

